package com.songlanyun.modules.gainsdonation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.coupon.model.vo.CouponEntityVo;
import com.songlanyun.modules.gains.entity.ShopOrderGains;
import com.songlanyun.modules.gainsdonation.model.ShopOrderGainsDonationVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.gainsdonation.dao.ShopOrderGainsDonationDao;
import com.songlanyun.modules.gainsdonation.entity.ShopOrderGainsDonationEntity;
import com.songlanyun.modules.gainsdonation.service.ShopOrderGainsDonationService;


@Service("shopOrderGainsDonationService")
public class ShopOrderGainsDonationServiceImpl extends ServiceImpl<ShopOrderGainsDonationDao, ShopOrderGainsDonationEntity> implements ShopOrderGainsDonationService {

    @Override
    public ShopOrderGainsDonationEntity create(ShopOrderGainsDonationEntity entity) {
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public ShopOrderGainsDonationEntity modify(ShopOrderGainsDonationEntity entity) {
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopOrderGainsDonationEntity> page = this.page(
                new Query<ShopOrderGainsDonationEntity>().getPage(params),
                new QueryWrapper<ShopOrderGainsDonationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryVoPage(Map<String, Object> params) {
        IPage<ShopOrderGainsDonationVo> page = this.baseMapper.queryVoPage(new Query<CouponEntityVo>().getPage(params), params);
        return new PageUtils(page);

    }

    @Override
    public ShopOrderGainsDonationEntity loadShopOrderGainsDonation(Long shopOrderGainsId) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<ShopOrderGainsDonationEntity>().eq(ShopOrderGainsDonationEntity::getShopOrderGainsId,shopOrderGainsId).last("limit 1"));
    }

    @Override
    public void refundDeduct(ShopOrderGains obj) {
        ShopOrderGainsDonationEntity shopOrderGainsDonationEntity = this.loadShopOrderGainsDonation(obj.getId());
        shopOrderGainsDonationEntity.setExistsRefund(true);

        if (obj.getGains().compareTo(BigDecimal.ZERO)>0 && shopOrderGainsDonationEntity.getDonationValue().compareTo(BigDecimal.ZERO) > 0) {
            shopOrderGainsDonationEntity.setRefundDeduct( MathUtils.setScale(obj.getRefundMoney().doubleValue() * shopOrderGainsDonationEntity.getDonationValue().doubleValue() / obj.getGains().doubleValue()));
            shopOrderGainsDonationEntity.setAbleGains(MathUtils.subtract(shopOrderGainsDonationEntity.getDonationValue(), shopOrderGainsDonationEntity.getRefundDeduct()));
        }
        this.modify(shopOrderGainsDonationEntity);

    }

}
